﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SharpDropBoxClient
{
    public class DBoxListing
    {
        public static DBoxListing FromDictionary(IDictionary<string, object> metaData)
        {
            var result = new DBoxListing();
            result.loadFromMetaData(metaData);
            return result;
        }

        private void loadFromMetaData(IDictionary<string, object> metaData)
        {
            if (metaData == null)
                return;
            if (metaData.ContainsKey("thumb_exists"))
                this.ThumbnailExists = (bool)metaData["thumb_exists"];
            if (metaData.ContainsKey("bytes"))
                this.SizeInBytes = (Int64)metaData["bytes"];
            if (metaData.ContainsKey("modified"))
                this.LastModified = (string)metaData["modified"];
            if (metaData.ContainsKey("path"))
                this.FullPath = (string)metaData["path"];
            if (metaData.ContainsKey("is_dir"))
                this.IsDirectory = (bool)metaData["is_dir"];
            if (metaData.ContainsKey("size"))
                this.Size = (string)metaData["size"];
            if (metaData.ContainsKey("root"))
                this.Root = (string)metaData["root"];
            if (metaData.ContainsKey("icon"))
                this.Icon = (string)metaData["icon"];
            if (metaData.ContainsKey("hash"))
                this.Hash = (string)metaData["hash"];
            if (metaData.ContainsKey("contents"))
            {
                Contents = new List<DBoxListing>();
                var contents = (ICollection<object>)metaData["contents"];
                foreach (var item in contents)
                {
                    var content = new DBoxListing();
                    content.loadFromMetaData((IDictionary<string, object>)item);
                    Contents.Add(content);
                }
            }
        }

        public bool ThumbnailExists { get; set; }

        public Int64 SizeInBytes { get; set; }

        public string LastModified { get; set; }

        public string FullPath { get; set; }

        public bool IsDirectory { get; set; }

        public string Size { get; set; }

        public string Root { get; set; }

        public string Icon { get; set; }

        public string Hash { get; set; }

        public string Name
        {
            get
            {
                return FullPath.Replace(@"\", @"/").Split('/').Last();
            }
        }

        public List<DBoxListing> Contents { get; set; }
    }
}